package me.gavin.base

import android.graphics.Outline
import android.graphics.Path
import android.graphics.drawable.Drawable
import android.view.View
import android.view.ViewOutlineProvider
import android.widget.EditText
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.databinding.BindingAdapter
import androidx.databinding.ObservableArrayList
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.appbar.AppBarLayout
import me.gavin.ext.afterTextChanged
import me.gavin.ext.hideSoftInput
import me.gavin.util.dp2pxF
import me.gavin.util.dp2px
import me.gavin.util.log


@BindingAdapter("cornerRadiusExt")
fun View.setOutlineProvider(radiusDp: Float) {
    clipToOutline = true
    outlineProvider = object : ViewOutlineProvider() {
        override fun getOutline(view: View, outline: Outline) {
            val radius = if (radiusDp == -1f) minOf(width, height) * 0.5f else radiusDp.dp2pxF()
            outline.setRoundRect(0, 0, width, height, radius)
        }
    }
}

@BindingAdapter("cornerRadiusLt", "cornerRadiusRt", "cornerRadiusRb", "cornerRadiusLb", requireAll = false)
fun View.setOutlineProvider(lt: Float, rt: Float, rb: Float, lb: Float) {
    clipToOutline = true
    outlineProvider = object : ViewOutlineProvider() {
        override fun getOutline(view: View, outline: Outline) {
            val ltr = if (lt == -1f) minOf(width, height) * 0.5f else lt.dp2pxF()
            val rtr = if (rt == -1f) minOf(width, height) * 0.5f else rt.dp2pxF()
            val rbr = if (rb == -1f) minOf(width, height) * 0.5f else rb.dp2pxF()
            val lbr = if (lb == -1f) minOf(width, height) * 0.5f else lb.dp2pxF()
            outline.setConvexPath(Path().apply {
                addRoundRect(0f, 0f, width.toFloat(), height.toFloat(),
                        floatArrayOf(ltr, ltr, rtr, rtr, rbr, rbr, lbr, lbr), Path.Direction.CW)
                "isConvex - $isConvex".log()
            })
        }
    }
    TODO()
}

@BindingAdapter("bindTextClear")
fun bindTextClear(v: View, editText: EditText) {
    v.setOnClickListener { editText.text = null }
    editText.afterTextChanged {
        v.visibility = if (editText.length() > 0) View.VISIBLE else View.GONE
    }
    v.visibility = if (editText.length() > 0) View.VISIBLE else View.GONE
}

@BindingAdapter("scrollWithHideSoftInput")
fun AppBarLayout.scrollWithHideSoftInput(enable: Boolean) {
    if (enable) {
        var offset = Int.MAX_VALUE
        addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { _, i ->
            if (offset != Int.MAX_VALUE && offset != i) hideSoftInput()
            offset = i
        })
    }
}

private fun Drawable?.resize(w: Float, h: Float) = this?.apply {
    if (w > 0 && h > 0) {
        setBounds(0, 0, w.dp2px(), h.dp2px())
    } else if (w > 0) {
        setBounds(0, 0, w.dp2px(), (w / intrinsicWidth * intrinsicHeight).dp2px())
    } else if (h > 0) {
        setBounds(0, 0, (h / intrinsicHeight * intrinsicWidth).dp2px(), h.dp2px())
    } else {
        setBounds(0, 0, intrinsicWidth, intrinsicHeight)
    }
}

@BindingAdapter(
        "drawableS", "drawableSW", "drawableSH",
        "drawableT", "drawableTW", "drawableTH",
        "drawableE", "drawableEW", "drawableEH",
        "drawableB", "drawableBW", "drawableBH",
        requireAll = false)
fun TextView.setCompoundDrawablesExt(
        ds: Drawable? = null, ws: Float = 0f, hs: Float = 0f,
        dt: Drawable? = null, wt: Float = 0f, ht: Float = 0f,
        de: Drawable? = null, we: Float = 0f, he: Float = 0f,
        db: Drawable? = null, wb: Float = 0f, hb: Float = 0f) {
    // ds?.setTint(0) todo
    setCompoundDrawables(ds?.resize(ws, hs), dt?.resize(wt, ht), de?.resize(we, he), db?.resize(wb, hb))
}

@androidx.databinding.BindingAdapter("dimensionRatio")
fun View.dimensionRatioExt(ratioExt: String) {
    updateLayoutParams<ConstraintLayout.LayoutParams> {
        dimensionRatio = ratioExt
    }
}